package org.absolutezero.model;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Version;


@Entity
@Table(name = "product")
public class Product {

	 private static Logger logger = Logger.getLogger(User.class.getName());
		
	 @Id
	 @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
	 
	 @Version
	 private Integer version;
     
     private String name;
     
     private Integer fat;
     
     private Integer protein;
     
     private Integer carbohydrate;
     
     private Integer calories;
     
     private Long unitWeight;
     
     private Double priceForUnit;
     
     @OneToMany(mappedBy = "product",cascade={CascadeType.ALL})
     private List<FridgeProduct> fridgeProducts;
               
     public List<FridgeProduct> getFridgeProducts() {
		return fridgeProducts;
	}

	public void setFridgeProducts(List<FridgeProduct> fridgeProducts) {
		this.fridgeProducts = fridgeProducts;
	}

	@OneToMany(mappedBy="product")
     private List<RecipeIngredient> ingredients;
          
     public List<RecipeIngredient> getIngredients(){
    	 return ingredients;
     }
     
     public void setIngredients(List<RecipeIngredient> ingredients){
    	 this.ingredients = ingredients;
     }
     
     
     public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public Integer getFat() {
		return fat;
	}


	public void setFat(Integer fat) {
		this.fat = fat;
	}


	public Integer getProtein() {
		return protein;
	}


	public void setProtein(Integer protein) {
		this.protein = protein;
	}


	public Integer getCarbohydrate() {
		return carbohydrate;
	}


	public void setCarbohydrate(Integer carbohydrate) {
		this.carbohydrate = carbohydrate;
	}


	public Integer getCalories() {
		return calories;
	}


	public void setCalories(Integer calories) {
		this.calories = calories;
	}


	public Long getUnitWeight() {
		return unitWeight;
	}


	public void setUnitWeight(Long unitWeight) {
		this.unitWeight = unitWeight;
	}


	public Double getPriceForUnit() {
		return priceForUnit;
	}


	public void setPriceForUnit(Double priceForUnit) {
		this.priceForUnit = priceForUnit;
	}


	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}
	
	
	public static List<Product> getAllProducts(){
		EntityManager em = entityManager();
		Query q = em.createQuery("SELECT p from Product p");
		
		return (List<Product>)q.getResultList();
	}
	
	
	public void persist() {
    	
	    EntityManager em = entityManager();
	    
	    try {
	    	
	      EntityTransaction tx  = em.getTransaction();
 	      tx.begin();
 	      Product p = em.merge(this);
 	      em.persist(p);
	      tx.commit();
	     
	    } finally {
	      em.close();
	    }
	  }

	public void remove() {
	    EntityManager em = entityManager();
	    try {
	
	      EntityTransaction tx  = em.getTransaction();
	      Product p = em.find(Product.class, this.id);
	      em.remove(p);
	      tx.commit();
	    } finally {
	      em.close();
	    }
	  }
	
	public static Product findProduct(Long id){
 		if (id == null) {
 		      return null;
	    }
	    EntityManager em = entityManager();
	    try {
	      Product product = em.find(Product.class, id);
	      return product;
	    } finally {
	      em.close();
	    }
 	}
	
	public Integer getVersion(){
 		return version;
 	}
	
	public void setVersion(Integer version){
		this.version = version;
	}
	
	/*@Override
 	public String toString(){
 		return String.format("User[%d, %d, %s, %b %b]", shelfId, owner.getUserId(), name, publicRead, publicWrite);
 	}*/
	
	 private static EntityManager entityManager(){
     	return EMF.get().createEntityManager();
     }
}
